* Create variables for event study analysis

replace rer = e7 if ccode=="IDN" & year>=1993 // ensure no change in series within a treated country.
drop lRER
gen lRER = log(rer)

* Indicator of devaluation and recent devaluation
gen app= 0
replace app=1 if largeDevaluation==1
replace app=. if rer==.

gen appr=0
replace appr=1 if largeDevaluation==1 & year>=1995 & ccode!="MEX"
replace appr=. if rer==.

foreach t of num 1/7{
gen app_l`t'=l`t'.app
gen appr_l`t'=l`t'.app
}
gen preRER = .
foreach t of num 1/10{
gen deval`t'=l`t'.app
gen devaf`t'=f`t'.app
replace preRER = lRER if devaf`t'==1
}
egen deva = rowtotal(app app_l1 app_l2 app_l3 app_l4) 

* Indicator that devaluation happens outside window:
bys ccode: egen totevents = total(app)
egen tempo = rowtotal(deval* devaf* app)
gen outside = 0
replace outside = 1 if totevents>tempo
gen outsideB = totevents- tempo

* Indicator no devaluation whatsoever:
gen AnyDeva=0
replace AnyDeva=1 if totevents>=1

* Adjustments due to transition of data in 1995 from JN to WIOD/OECD for some events
gen T=0
replace T=1 if largeDevaluation==1  & year<=2000 & year>1995 & importshare!=importshare2 // WIOD/OECD available
local tt= 5
sort id year
foreach t of num 1/`tt'{
gen Tf`t'=f`t'.T
}
egen Tftot = rowtotal(Tf*) 	
foreach var in importshare2 {
replace `var' =. if year<1995 & Tftot>=1
}
	
* Create normyear (time since devaluation)
gen yearb = year * app
bys ccode: egen depreciationyear = total(yearb)
drop yearb
gen normyear= year-depreciationyear

egen  postdevawindow5   = rowtotal(app deval1 deval2 deval3 deval4 deval5)
egen  postdevawindow10  = rowtotal(app deval*)
egen  devawindow5       = rowtotal(devaf5 devaf4 devaf3 devaf2 devaf1 app deval1 deval2 deval3 deval4 deval5)
egen  devawindow10     	= rowtotal(devaf* app deval*) 
gen   trendwindow5      = devawindow5 * normyear/10 
gen   trendwindow10     = devawindow10 * normyear/10 
gen   trendpostwindow5  = postdevawindow5 * normyear/10 
gen   trendpostwindow10 = postdevawindow10 * normyear/10
gen  predevawindow5b = devaf5 + devaf4 + devaf3+ devaf2 +devaf1 
gen  postdevawindow5b = app +deval1+ deval2 +deval3 +deval4+deval5
gen  postdevawindow10b = app +deval1+ deval2 +deval3 +deval4+ deval5+ deval6+ deval7+ deval8+ deval9+ deval10
gen   devawindow5b = devaf5 + devaf4 + devaf3+ devaf2 +devaf1 +app +deval1+ deval2 +deval3 +deval4+ deval5

* Update some variables:
drop yearD D anycurrency anycurrency_recent Recent year80D year90D year00D ///
	 year80J year90J year00J yearD  yearJ 

bys ccode: egen anycurrency = total(app)
bys ccode: egen anycurrency_recent = total(appr)
gen D=0
replace D=1 if anycurrency>=1
gen Recent=0
replace Recent=1 if anycurrency_recent>=1

* decadal and group-specific linear time trends
gen year80D = year*decade80 * D
gen year90D = year*decade90 * D
gen year00D = year*decade00 * D
gen year80J = year*decade80 * Recent
gen year90J = year*decade90 * Recent
gen year00J = year*decade00 * Recent

* group-specific linear time trends
gen yearD = year * D
gen yearJ = year * Recent
